import Vue from 'vue'
import VueRouter from 'vue-router'
import ConstantRouter from './constantRouter'
import global_ from './../components/common'
/*
import file from '@/components/file'
import login from '@/components/login'
import comment from '@/components/comment'
import share from '@/components/share'
import mainpage from '@/views/Mainpage'
*/
Vue.use(VueRouter)

const Routerconfig={
    mode:'history',
    routes: ConstantRouter,
    base: '/platform/',
    
}

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

function NewRouter(){
    return new VueRouter(Routerconfig);
}


//var ISLOGIN = true
const router=NewRouter({
  /*routes:[
    {
      path: '/',
      name: 'login',
      component: login,
    },
    {
      path: '/platform/Mainpage',
      name: 'Mainpage',
      component:mainpage,
     
    },
    {
      path: '/platform/Mainpage/file',
      name: 'file',
      component: file
    },
    {
      path: '/platform/Mainpage/share',
      name: 'share',
      component: share
    },
    {
      path: '/platform/Mainpage/comment',
      name: 'comment',
      component: comment
    }
  ]*/
  
})

router.beforeEach((to, from, next) => {   //全局全局前置守卫
  //to : 将要进入的目标路由对象
  //from : 即将离开的目标路由对象
  //执行跳转的下一步钩子
  console.log(to)
  console.log(from)
  console.log("页面一切正常")
  if(global_.ISLOGIN){next()}
  else{
  if(to.path != '/'){next({path:'/'})  //否则跳转到登录页面
  }
  else{ //如果是登录页面
    //if(global_.ISLOGIN)next({path:'/Mainpage/'}) //已登录就跳转到首页
    //else  
    next()  //否则正常进入登录页面
  }
  }
})


export default router